//
// Created by castle on 2020/9/10.
//

#include "GameLogic.h"
#include <iostream>

GameLogic::GameLogic()
{
	for(auto & cell : cells)
		cell = Free;
	step = 0;
}

GameLogic::GameStatue GameLogic::StatueCheck()
{
	if(cells[0] != Free && cells[0] == cells[1] && cells[0] == cells[2])
		return (GameStatue)cells[0];
	if(cells[0] != Free  && cells[0] == cells[3] && cells[0] == cells[6])
		return (GameStatue)cells[0];
	if(cells[0] != Free  && cells[0] == cells[4] && cells[0] == cells[8])
		return (GameStatue)cells[0];
	if(cells[1] != Free  && cells[1] == cells[4] && cells[1] == cells[7])
		return (GameStatue)cells[0];
	if(cells[2] != Free  && cells[2] == cells[5] && cells[2] == cells[8])
		return (GameStatue)cells[0];
	if(cells[2] != Free  && cells[2] == cells[4] && cells[2] == cells[6])
		return (GameStatue)cells[0];
	if(cells[3] != Free  && cells[3] == cells[4] && cells[3] == cells[5])
		return (GameStatue)cells[0];
	if(cells[6] != Free  && cells[6] == cells[7] && cells[6] == cells[8])
		return (GameStatue)cells[0];
	for(auto & cell : cells)
		if(cell == Free)
			return Playing;
	return Draw;
}

GameLogic::GameStatue GameLogic::PickCell(int index)
{
	if(cells[index] != Free)
		std::cerr << "There is already a pawn here! " << std::endl;
	else
	{
		cells[index] = (CellType)GetPlayerNow();
		step++;
		step%=2;
	}
	return StatueCheck();
}
